package com.stake.web.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.stake.entity.Questions;
import com.stake.model.QuestForm;

@Controller
@RequestMapping("/admin") 
@SessionAttributes("questForm") 
public class AdminController {
	
	@Autowired
	@Qualifier("persistenceManagerFactory") 
	PersistenceManagerFactory persistenceManagerFactory;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/updateQuestions.htm") 
	public ModelAndView displayQuestionsForm(@ModelAttribute("questForm") QuestForm form){
		Map<String, Object > model = new HashMap<String, Object>();
		
		
		PersistenceManager manager = persistenceManagerFactory.getPersistenceManager();
		Query query = manager.newQuery(Questions.class);
		List<Questions> questions = (List<Questions>) query.execute();
		questions = (List<Questions>) manager.detachCopyAll(questions);
		
		Collections.sort(questions, new Comparator<Questions>() {

			@Override
			public int compare(Questions o1, Questions o2) {
				return o1.getId().compareTo(o2.getId());
			}
			
		});
		form = new QuestForm();
		form.setQuestions(questions);
		model.put("questForm", form);
		return new ModelAndView("updateQuestions", model);
	}
	
	@RequestMapping("/updateQuestionsSubmit.htm") 
	public @ResponseBody String updateQuestions(@ModelAttribute("questForm") QuestForm form, HttpServletRequest request){
		List<Questions> updatedQs = form.getQuestions();
		
		System.out.println("list received from form : " + updatedQs.size());
		PersistenceManager manager = persistenceManagerFactory.getPersistenceManager();
		
		manager.close();
		for(Questions q : updatedQs){
			System.out.println(" boolean non linked user : " + q.getNonLinkedInUser());
			if(q.getNonLinkedInUser() == null){
				q.setNonLinkedInUser(Boolean.FALSE);
			}
			if(q.getKey() != null){
				manager = persistenceManagerFactory.getPersistenceManager();
				Questions oldObj = manager.getObjectById(Questions.class, q.getKey());
				System.out.println("old object found : " + oldObj.getQuestionDesc());
				manager.deletePersistent(oldObj);
				System.out.println("old object deleted"); 
				manager.close();
			} else {
				System.out.println("a new entry here : " + q.getQuestionDesc());
			}
			manager = persistenceManagerFactory.getPersistenceManager();
			manager.makePersistent(q);
			manager.close();
		}
		return "success"; 
	}
	
	@ModelAttribute("questForm")
	public QuestForm populateQuestForm(){
		return new QuestForm();
	}
}
